﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Threading;
using System.Globalization;
using Nivdal.AuxCode;

namespace Nivdal
{
    public partial class SearchTransferMarket : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!HttpContext.Current.User.Identity.IsAuthenticated)
                Response.Redirect("~/Default.aspx");
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            OnlineUser ou = (OnlineUser)Session["User"];
            this.Title = Resources.Resource.Default_PageTitle + ", " + Resources.Resource.SearchTransferMarket_PageTitle;
            //this.Title = Resources.Resource.SearchTransferMarketPageTitle + " - " + GND.ReturnAppTitle(); 
            if (ou.TeamId == null)
                Response.Redirect("~/GeneralMessage.aspx?MessageString=M74ggdizz5wfeh");

            // Using this place to send pending emails:
            try  { GND.SendPendingMails(); }
            catch { }

            ErrorLabelPanel.Visible = (ErrLabel.Text.Length > 0);
            if (!IsPostBack)
            {
                StrongFootDataSource.SelectParameters.Add("Language", ou.Language);
            }

            if (!IsPostBack && Session["SearchTransferMarketValues"] != null)
            {
                TransferMarket tm = (TransferMarket)Session["SearchTransferMarketValues"];
                InstinctsMinTextBox.Text = tm.InstinctsMin;
                InstinctsMaxTextBox.Text = tm.InstinctsMax;
                GoalkeepingMinTextBox.Text = tm.GoalkeepingMin;
                GoalkeepingMaxTextBox.Text = tm.GoalkeepingMax;
                PositioningMinTextBox.Text = tm.PositioningMin;
                PositioningMaxTextBox.Text = tm.PositioningMax;
                StaminaMinTextBox.Text = tm.StaminaMin;
                StaminaMaxTextBox.Text = tm.StaminaMax;
                PowerMinTextBox.Text = tm.PowerMin;
                PowerMaxTextBox.Text = tm.PowerMax;
                TacklingMinTextBox.Text = tm.TacklingMin;
                TacklingMaxTextBox.Text = tm.TacklingMax;
                HeadingMinTextBox.Text = tm.HeadingMin;
                HeadingMaxTextBox.Text = tm.HeadingMax;
                QuicknessMinTextBox.Text = tm.QuicknessMin;
                QuicknessMaxTextBox.Text = tm.QuicknessMax;
                TechniqueMinTextBox.Text = tm.TechniqueMin;
                TechniqueMaxTextBox.Text = tm.TechniqueMax;
                ShortPassingMinTextBox.Text = tm.ShortPassingMin;
                ShortPassingMaxTextBox.Text = tm.ShortPassingMax;
                LongPassingMinTextBox.Text = tm.LongPassingMin;
                LongPassingMaxTextBox.Text = tm.LongPassingMax;
                ShootingMinTextBox.Text = tm.ShootingMin;
                ShootingMaxTextBox.Text = tm.ShootingMax;
                AgeMinTextBox.Text = tm.AgeMin;
                AgeMaxTextBox.Text = tm.AgeMax;
                CharismaMinTextBox.Text = tm.CharismaMin;
                CharismaMaxTextBox.Text = tm.CharismaMax;
                ExperienceMinTextBox.Text = tm.ExperienceMin;
                ExperienceMaxTextBox.Text = tm.ExperienceMax;
                NationalityDropDownList.SelectedValue = tm.Nationality;
                PlyerTyperadio.SelectedIndex = Int32.Parse(tm.PlyerTyperadio);
                DeadlineDropDownList.SelectedIndex = Int32.Parse(tm.Deadline);
                CurrentBidMinTextBox.Text = tm.CurrentBidMin;
                CurrentBidMaxTextBox.Text = tm.CurrentBidMax;
                GKCheckBox.Checked = tm.PosGK == "1";
                DefCheckBox.Checked = tm.PosDef == "1";
                MidCheckBox.Checked = tm.PosMid == "1";
                ForCheckBox.Checked = tm.PosFor == "1";
                LeftCheckBox.Checked = tm.PosLeft == "1";
                CentralCheckBox.Checked = tm.PosCen == "1";
                RightCheckBox.Checked = tm.PosRight == "1";
                StrongFootDDL.SelectedValue = tm.StrongFoot;
               
                //Response.Write(tm.IsGK);
            }

            if (Session["SearchTransferMarketValues"] == null)
                Session["SearchTransferMarketValues"] = new Nivdal.AuxCode.TransferMarket
                {
                    InstinctsMin = "0",
                    InstinctsMax = "100",
                    GoalkeepingMin = "0",
                    GoalkeepingMax = "100",
                    PositioningMin = "0",
                    PositioningMax = "100",
                    StaminaMin = "0",
                    StaminaMax = "100",
                    PowerMin = "0",
                    PowerMax = "100",
                    TacklingMin = "0",
                    TacklingMax = "100",
                    HeadingMin = "0",
                    HeadingMax = "100",
                    QuicknessMin = "0",
                    QuicknessMax = "100",
                    TechniqueMin = "0",
                    TechniqueMax = "100",
                    ShortPassingMin = "0",
                    ShortPassingMax = "100",
                    LongPassingMin = "0",
                    LongPassingMax = "100",
                    ShootingMin = "0",
                    ShootingMax = "100",
                    AgeMin = "16",
                    AgeMax = "40",
                    CharismaMin = "0",
                    CharismaMax = "100",
                    ExperienceMin = "0",
                    ExperienceMax = "100",
                    Nationality = "10",
                    PlyerTyperadio = "3",
                    Deadline = "0",
                    CurrentBidMax = "99999999",
                    CurrentBidMin = "0",
                    PosCen = "1",
                    PosDef = "1",
                    PosFor = "1",
                    PosGK = "1",
                    PosLeft = "1",
                    PosMid = "1",
                    PosRight = "1",
                    StrongFoot="0"
                };

            
        }

        protected void NullifyButton_Click(object sender, EventArgs e)
        {
            if (Session["SearchTransferMarketValues"] != null)
            {
                TransferMarket tm = (TransferMarket)Session["SearchTransferMarketValues"];
                tm.InstinctsMin = "0";
                tm.InstinctsMax = "100";
                tm.GoalkeepingMin = "0";
                tm.GoalkeepingMax = "100";
                tm.PositioningMin = "0";
                tm.PositioningMax = "100";
                tm.StaminaMin = "0";
                tm.StaminaMax = "100";
                tm.PowerMin = "0";
                tm.PowerMax = "100";
                tm.TacklingMin = "0";
                tm.TacklingMax = "100";
                tm.HeadingMin = "0";
                tm.HeadingMax = "100";
                tm.QuicknessMin = "0";
                tm.QuicknessMax = "100";
                tm.TechniqueMin = "0";
                tm.TechniqueMax = "100";
                tm.ShortPassingMin = "0";
                tm.ShortPassingMax = "100";
                tm.LongPassingMin = "0";
                tm.LongPassingMax = "100";
                tm.ShootingMin = "0";
                tm.ShootingMax = "100";
                tm.AgeMin = "16";
                tm.AgeMax = "40";
                tm.CharismaMin = "0";
                tm.CharismaMax = "100";
                tm.ExperienceMin = "0";
                tm.ExperienceMax = "100";
                tm.Nationality = "10";
                tm.PlyerTyperadio = "3";
                tm.Deadline = "0";
                tm.CurrentBidMin = "0";
                tm.CurrentBidMax = "99999999";
                tm.PosGK = "1";
                tm.PosDef = "1";
                tm.PosMid = "1";
                tm.PosFor = "1";
                tm.PosLeft = "1";
                tm.PosCen = "1";
                tm.PosRight = "1";
                tm.StrongFoot = "0";
                Response.Redirect("SearchTransferMarket.aspx");
                

            }

        }

        protected void SubmitButton_Click(object sender, EventArgs e)
        {
            bool IsInputOk = true;
            string MinInstincts = this.InstinctsMinTextBox.Text.Trim();
            if (MinInstincts.Length == 0) MinInstincts = "0";
            string MaxInstincts = this.InstinctsMaxTextBox.Text.Trim();
            if (MaxInstincts.Length == 0) MaxInstincts = "100";
            string MinGoalkeeping = this.GoalkeepingMinTextBox.Text.Trim();
            if (MinGoalkeeping.Length == 0) MinGoalkeeping = "0";
            string MaxGoalkeeping = this.GoalkeepingMaxTextBox.Text.Trim();
            if (MaxGoalkeeping.Length == 0) MaxGoalkeeping = "100";
            string MinPositioning = this.PositioningMinTextBox.Text.Trim();
            if (MinPositioning.Length == 0) MinPositioning = "0";
            string MaxPositioning = this.PositioningMaxTextBox.Text.Trim();
            if (MaxPositioning.Length == 0) MaxPositioning = "100";
            string MinStamina = this.StaminaMinTextBox.Text.Trim();
            if (MinStamina.Length == 0) MinStamina = "0";
            string MaxStamina = this.StaminaMaxTextBox.Text.Trim();
            if (MaxStamina.Length == 0) MaxStamina = "100";
            string MinPower = this.PowerMinTextBox.Text.Trim();
            if (MinPower.Length == 0) MinPower = "0";
            string MaxPower = this.PowerMaxTextBox.Text.Trim();
            if (MaxPower.Length == 0) MaxPower = "100";
            string MinTackling = this.TacklingMinTextBox.Text.Trim();
            if (MinTackling.Length == 0) MinTackling = "0";
            string MaxTackling = this.TacklingMaxTextBox.Text.Trim();
            if (MaxTackling.Length == 0) MaxTackling = "100";
            string MinHeading = this.HeadingMinTextBox.Text.Trim();
            if (MinHeading.Length == 0) MinHeading = "0";
            string MaxHeading = this.HeadingMaxTextBox.Text.Trim();
            if (MaxHeading.Length == 0) MaxHeading = "100";
            string MinQuickness = this.QuicknessMinTextBox.Text.Trim();
            if (MinQuickness.Length == 0) MinQuickness = "0";
            string MaxQuickness = this.QuicknessMaxTextBox.Text.Trim();
            if (MaxQuickness.Length == 0) MaxQuickness = "100";
            string MinTechnique = this.TechniqueMinTextBox.Text.Trim();
            if (MinTechnique.Length == 0) MinTechnique = "0"; 
            string MaxTechnique = this.TechniqueMaxTextBox.Text.Trim();
            if (MaxTechnique.Length == 0) MaxTechnique = "100";
            string MinShortPassing = this.ShortPassingMinTextBox.Text.Trim();
            if (MinShortPassing.Length == 0) MinShortPassing = "0"; 
            string MaxShortPassing = this.ShortPassingMaxTextBox.Text.Trim();
            if (MaxShortPassing.Length == 0) MaxShortPassing = "100"; 
            string MinLongPassing = this.LongPassingMinTextBox.Text.Trim();
            if (MinLongPassing.Length == 0) MinLongPassing = "0"; 
            string MaxLongPassing = this.LongPassingMaxTextBox.Text.Trim();
            if (MaxLongPassing.Length == 0) MaxLongPassing = "100"; 
            string MinShooting = this.ShootingMinTextBox.Text.Trim();
            if (MinShooting.Length == 0) MinShooting = "0"; 
            string MaxShooting = this.ShootingMaxTextBox.Text.Trim();
            if (MaxShooting.Length == 0) MaxShooting = "100"; 
            string MinAge = this.AgeMinTextBox.Text.Trim();
            if (MinAge.Length <= 1) MinAge = "16"; 
            string MaxAge = this.AgeMaxTextBox.Text.Trim();
            if (MaxAge.Length <= 1) MaxAge = "40";
            string MinCharisma = this.CharismaMinTextBox.Text.Trim();
            if (MinCharisma.Length == 0) MinCharisma = "0";
            string MaxCharisma = this.CharismaMaxTextBox.Text.Trim();
            if (MaxCharisma.Length == 0) MaxCharisma = "100";
            string MinExperience = this.ExperienceMinTextBox.Text.Trim();
            if (MinExperience.Length == 0) MinExperience = "0";
            string MaxExperience = this.ExperienceMaxTextBox.Text.Trim();
            if (MaxExperience.Length == 0) MaxExperience = "100"; 

 
            string MinBid = this.CurrentBidMinTextBox.Text.Trim();
            if (MinBid.Length == 0) MinBid = "0";
            string MaxBid = this.CurrentBidMaxTextBox.Text.Trim();
            if (MaxBid.Length == 0) MaxBid = "99999999";
            string Nationality = this.NationalityDropDownList.SelectedValue;
            string PlayerType = this.PlyerTyperadio.SelectedIndex.ToString();
            string Deadline = this.DeadlineDropDownList.SelectedValue;
            string PosGK = GKCheckBox.Checked ? "1" : "0";
            string PosDef = DefCheckBox.Checked ? "1" : "0";
            string PosMid = MidCheckBox.Checked ? "1" : "0";
            string PosFor = ForCheckBox.Checked ? "1" : "0";
            string PosLeft = LeftCheckBox.Checked ? "1" : "0";
            string PosCen = CentralCheckBox.Checked ? "1" : "0";
            string PosRight = RightCheckBox.Checked ? "1" : "0";
            string StrongFoot = StrongFootDDL.SelectedValue;


            if (Convert.ToDecimal(MinInstincts) > Convert.ToDecimal(MaxInstincts))
            {
                ErrLabel.Text = Resources.Resource.SearchTransferMarket_MinInstinctsBiggerThenMaxInstincts;
                IsInputOk = false;
            }

            if ((Convert.ToDecimal(MinGoalkeeping) > Convert.ToDecimal(MaxGoalkeeping)) & (IsInputOk == true))
            {
                ErrLabel.Text = Resources.Resource.SearchTransferMarket_MinGoalkeepingBiggerThenMaxGoalkeeping;
                IsInputOk = false;
            }

            if ((Convert.ToDecimal(MinPositioning) > Convert.ToDecimal(MaxPositioning)) & (IsInputOk == true))
            {
                ErrLabel.Text = Resources.Resource.SearchTransferMarket_MinPositioningBiggerThenMaxPositioning;
                IsInputOk = false;
            }


            if ((Convert.ToDecimal(MinStamina) > Convert.ToDecimal(MaxStamina)) & (IsInputOk == true))
            {
                ErrLabel.Text = Resources.Resource.SearchTransferMarket_MinStaminaBiggerThenMaxStamina;
                IsInputOk = false;
            }

            if ((Convert.ToDecimal(MinPower) > Convert.ToDecimal(MaxPower)) & (IsInputOk == true))
            {
                ErrLabel.Text = Resources.Resource.SearchTransferMarket_MinPowerBiggerThenMaxPower;
                IsInputOk = false;
            }

            if ((Convert.ToDecimal(MinTackling) > Convert.ToDecimal(MaxTackling)) & (IsInputOk == true))
            {
                ErrLabel.Text = Resources.Resource.SearchTransferMarket_MinTacklingBiggerThenMaxTackling;
                IsInputOk = false;
            }

            if ((Convert.ToDecimal(MinHeading) > Convert.ToDecimal(MaxHeading)) & (IsInputOk == true))
            {
                ErrLabel.Text = Resources.Resource.SearchTransferMarket_MinHeadingBiggerThenMaxHeading;
                IsInputOk = false;
            }

            if ((Convert.ToDecimal(MinQuickness) > Convert.ToDecimal(MaxQuickness)) & (IsInputOk == true))
            {
                ErrLabel.Text = Resources.Resource.SearchTransferMarket_MinQuicknessBiggerThenMaxQuickness;
                IsInputOk = false;
            }


            if ((Convert.ToDecimal(MinShortPassing) > Convert.ToDecimal(MaxShortPassing)) & (IsInputOk == true))
            {
                ErrLabel.Text = Resources.Resource.SearchTransferMarket_MinShortPassingBiggerThenMaxShortPassing;
                IsInputOk = false;
            }

            if ((Convert.ToDecimal(MinLongPassing) > Convert.ToDecimal(MaxLongPassing)) & (IsInputOk == true))
            {
                ErrLabel.Text = Resources.Resource.SearchTransferMarket_MinLongPassingBiggerThenMaxLongPassing;
                IsInputOk = false;
            }

            if ((Convert.ToDecimal(MinShooting) > Convert.ToDecimal(MaxShooting)) & (IsInputOk == true))
            {
                ErrLabel.Text = Resources.Resource.SearchTransferMarket_MinShootinBgiggerThenMaxShooting;
                IsInputOk = false;
            }


            if ((Convert.ToDecimal(MinCharisma) > Convert.ToDecimal(MaxCharisma)) & (IsInputOk == true))
            {
                ErrLabel.Text = Resources.Resource.SearchTransferMarket_MinCharismaBiggerThenMaxCharisma;
                IsInputOk = false;
            }


            if ((Convert.ToDecimal(MinExperience) > Convert.ToDecimal(MaxExperience)) & (IsInputOk == true))
            {
                ErrLabel.Text = Resources.Resource.SearchTransferMarket_MinExperienceBiggerThenMaxExperience;
                IsInputOk = false;
            }


            if ((Convert.ToDecimal(MinAge) > Convert.ToDecimal(MaxAge)) & (IsInputOk == true))
            {
                ErrLabel.Text = Resources.Resource.SearchTransferMarket_MinAgeBiggerThenMaxAge;
                IsInputOk = false;
            }

            if ((Convert.ToDecimal(MinBid) > Convert.ToDecimal(MaxBid)) & (IsInputOk == true))
            {
                ErrLabel.Text = Resources.Resource.SearchTransferMarket_MinBidBiggerThenMaxBid;
                IsInputOk = false;
            }


            if (IsInputOk)
            {
                if (Session["SearchTransferMarketValues"] != null)
                {
                    TransferMarket tm = (TransferMarket)Session["SearchTransferMarketValues"];

                    tm.InstinctsMin = MinInstincts;
                    tm.InstinctsMax = MaxInstincts;
                    tm.GoalkeepingMin = MinGoalkeeping;
                    tm.GoalkeepingMax = MaxGoalkeeping;
                    tm.PositioningMin = MinPositioning;
                    tm.PositioningMax = MaxPositioning;
                    tm.StaminaMin = MinStamina;
                    tm.StaminaMax = MaxStamina;
                    tm.PowerMin = MinPower;
                    tm.PowerMax = MaxPower;
                    tm.TacklingMin = MinTackling;
                    tm.TacklingMax = MaxTackling;
                    tm.HeadingMin = MinHeading;
                    tm.HeadingMax = MaxHeading;
                    tm.QuicknessMin = MinQuickness;
                    tm.QuicknessMax = MaxQuickness;
                    tm.TechniqueMin = MinTechnique;
                    tm.TechniqueMax = MaxTechnique;
                    tm.ShortPassingMin = MinShortPassing;
                    tm.ShortPassingMax = MaxShortPassing;
                    tm.LongPassingMin = MinLongPassing;
                    tm.LongPassingMax = MaxLongPassing;
                    tm.ShootingMin = MinShooting;
                    tm.ShootingMax = MaxShooting;
                    tm.AgeMin = MinAge;
                    tm.AgeMax = MaxAge;
                    tm.CharismaMin = MinCharisma;
                    tm.CharismaMax = MaxCharisma;
                    tm.ExperienceMin = MinExperience;
                    tm.ExperienceMax = MaxExperience;
                    tm.Nationality = Nationality;
                    tm.PlyerTyperadio = PlayerType;
                    tm.Deadline = Deadline;
                    tm.CurrentBidMin = MinBid;
                    tm.CurrentBidMax = MaxBid;
                    tm.PosGK = PosGK;
                    tm.PosDef = PosDef;
                    tm.PosMid = PosMid;
                    tm.PosFor = PosFor;
                    tm.PosLeft = PosLeft;
                    tm.PosCen = PosCen;
                    tm.PosRight = PosRight;
                    tm.StrongFoot = StrongFoot;
                }



                Response.Redirect("ListTransferListedPlayers.aspx");
            }

        }

        protected override void InitializeCulture()
        {
            OnlineUser ou = (OnlineUser)Session["User"];
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(ou.UICulture);
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ou.Culture);
        }

        protected bool IsRTL()
        {
            if ((Master.Master.FindControl("DirectionPanel") as Panel).Direction == ContentDirection.RightToLeft)
                return true;
            return false;
        }
    }
}